package city.spring.controller;

import city.spring.domain.entity.HealthCheckEntity;
import city.spring.service.HealthCheckService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：体检登记
 *
 * @author pxm
 * @date 2020-06-17 16:21:46
 */
@RestController
@RequestMapping("/health_check")
public class HealthCheckController extends ApiController {
    private final HealthCheckService healthCheckService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<HealthCheckEntity, ?>> entityOrderFields;

    public HealthCheckController(HealthCheckService healthCheckService) {
        this.healthCheckService = healthCheckService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(HealthCheckEntity::getId);
        entityOrderFields.add(HealthCheckEntity::getPerson);
        entityOrderFields.add(HealthCheckEntity::getCheckTime);
        entityOrderFields.add(HealthCheckEntity::getHospital);
        entityOrderFields.add(HealthCheckEntity::getPhotos);
        entityOrderFields.add(HealthCheckEntity::getResults);
        entityOrderFields.add(HealthCheckEntity::getRegistrant);
        entityOrderFields.add(HealthCheckEntity::getGmtCreate);
        entityOrderFields.add(HealthCheckEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>体检登记</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, HealthCheckEntity entity) {
        LambdaQueryChainWrapper<HealthCheckEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>体检登记</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, HealthCheckEntity entity) {
        LambdaQueryChainWrapper<HealthCheckEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<HealthCheckEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>体检登记</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(healthCheckService.getById(id));
    }

    /**
     * 添加一个 <strong>体检登记</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody HealthCheckEntity entity) {
        healthCheckService.saveHealthCheck(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>体检登记</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody HealthCheckEntity entity) {
        entity.setId(id);
        healthCheckService.updateHealthCheck(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>体检登记</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        healthCheckService.deleteHealthCheck(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>体检登记</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        healthCheckService.deleteHealthCheck(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<HealthCheckEntity> buildLambdaQuery(HealthCheckEntity entity) {
        LambdaQueryChainWrapper<HealthCheckEntity> lambdaQuery = healthCheckService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), HealthCheckEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPerson()), HealthCheckEntity::getPerson, entity.getPerson());
        lambdaQuery.eq(entity.getCheckTime() != null, HealthCheckEntity::getCheckTime, entity.getCheckTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getHospital()), HealthCheckEntity::getHospital, entity.getHospital());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhotos()), HealthCheckEntity::getPhotos, entity.getPhotos());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getResults()), HealthCheckEntity::getResults, entity.getResults());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRegistrant()), HealthCheckEntity::getRegistrant, entity.getRegistrant());
        lambdaQuery.eq(entity.getGmtCreate() != null, HealthCheckEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, HealthCheckEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

}
